package com.lwb.educenter.controller;


import com.lwb.commonutils.JwtUtils;
import com.lwb.commonutils.Row;
import com.lwb.commonutils.UcenterMemberVo;
import com.lwb.educenter.entity.UcenterMember;
import com.lwb.educenter.entity.vo.RegisterVo;
import com.lwb.educenter.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/educenter/ucenter-member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping("login")
    public Row login(@RequestBody UcenterMember ucenterMember){
        String token = ucenterMemberService.login(ucenterMember);
        return Row.SUCCESS().data("token",token);
    }

    //注册
    @PostMapping("register")
    public Row register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return Row.SUCCESS();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public Row getMemberInfo(HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember ucenterMemberServiceById = ucenterMemberService.getById(memberIdByJwtToken);
        return Row.SUCCESS().data("userInfo",ucenterMemberServiceById);
    }

    //根据token值获取用户信息
    @GetMapping("getUserInfo/{id}")
    public UcenterMemberVo getUserInfo(@PathVariable String id){
        System.out.println("到达这里！！"+id);
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        UcenterMemberVo ucenterMemberVo = new UcenterMemberVo();
        ucenterMemberVo.setMemberId(ucenterMember.getId());
        ucenterMemberVo.setNickname(ucenterMember.getNickname());
        ucenterMemberVo.setMemberAvatar(ucenterMember.getAvatar());
        return ucenterMemberVo;
    }

    //查询一天注册的人数
    @GetMapping("countRegister/{day}")
    public Row countRegister(@PathVariable String day){
        Integer count = ucenterMemberService.countRegister(day);
        return Row.SUCCESS().data("count",count);
    }

}

